//
//  QFProgressHUD.m
//  lehuotest
//
//  Created by ice on 2017/1/10.
//  Copyright © 2017年 QF. All rights reserved.
//

#import "LHProgressHUD.h"
#import "LHIndefiniteAnimatedView.h"

#define hudHeight 100

#define IndefiniteView_H 40

@interface LHProgressHUD()

@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UIView *indefiniteAnimatedView;

@property (nonatomic, strong) UIImage *image;
@end

@implementation LHProgressHUD


-(instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self){
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(frame.size.width/2-hudHeight/2, frame.size.height/2-hudHeight/2, hudHeight, hudHeight)];
        self.contentView.backgroundColor = [UIColor clearColor];
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = 3.f;
        [self addSubview:self.contentView];
        [self initUI];
    }
    return self;
}

-(void) initUI
{
    [self setUpViews];
}


-(void) setUpViews
{
    [self.contentView addSubview:self.indefiniteAnimatedView];
}
- (UIView*)indefiniteAnimatedView {
    if(_indefiniteAnimatedView && ![_indefiniteAnimatedView isKindOfClass:[LHIndefiniteAnimatedView class]]){
        [_indefiniteAnimatedView removeFromSuperview];
        _indefiniteAnimatedView = nil;
    }
    
    if(!_indefiniteAnimatedView){
        _indefiniteAnimatedView = [[LHIndefiniteAnimatedView alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width/2-IndefiniteView_H/2, self.contentView.frame.size.height/2-IndefiniteView_H/2, IndefiniteView_H, IndefiniteView_H)];
    }
    
    // Update styling
    LHIndefiniteAnimatedView *indefiniteAnimatedView = (LHIndefiniteAnimatedView*)_indefiniteAnimatedView;
    indefiniteAnimatedView.strokeColor = [UIColor lightGrayColor];//self.foregroundColorForStyle;
    indefiniteAnimatedView.strokeThickness = 1;//self.ringThickness;
    indefiniteAnimatedView.radius = 15;//self.statusLabel.text ? self.ringRadius : self.ringNoTextRadius;
     [_indefiniteAnimatedView sizeToFit];
    return _indefiniteAnimatedView;
}

-(void) setImage:(UIImage *)image
{
    _image = image;
    LHIndefiniteAnimatedView *indefiniteAnimatedView = (LHIndefiniteAnimatedView*)_indefiniteAnimatedView;
    indefiniteAnimatedView.image = image;
}


+(void) showHudToView:(UIView *)view
{
    if (view == nil){
        view = [UIApplication sharedApplication].keyWindow;
    }
    LHProgressHUD *hud = [[LHProgressHUD alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    hud.image = [UIImage imageNamed:@"icon_child_01"];
    [view addSubview:hud];
    [self hideHUDForView:view afterDelay:2];
}

+(void) hideHUDForView:(UIView *)view afterDelay:(NSTimeInterval)delay
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hideHUDForView:view];
    });
}

+ (BOOL)hideHUDForView:(UIView *)view {
    LHProgressHUD *hud = [self HUDForView:view];
    if (hud != nil) {
        [UIView animateWithDuration:0.3 animations:^{
            hud.alpha = 0;
        } completion:^(BOOL finished) {
            [hud removeFromSuperview];
        }];
        return YES;
    }
    return NO;
}

+ (LHProgressHUD *)HUDForView:(UIView *)view {
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            return (LHProgressHUD *)subview;
        }
    }
    return nil;
}
//- (void)hideAnimated:(BOOL)animated {
//    [self.graceTimer invalidate];
//    self.useAnimation = animated;
//    self.finished = YES;
//    // If the minShow time is set, calculate how long the HUD was shown,
//    // and postpone the hiding operation if necessary
//    if (self.minShowTime > 0.0 && self.showStarted) {
//        NSTimeInterval interv = [[NSDate date] timeIntervalSinceDate:self.showStarted];
//        if (interv < self.minShowTime) {
//            NSTimer *timer = [NSTimer timerWithTimeInterval:(self.minShowTime - interv) target:self selector:@selector(handleMinShowTimer:) userInfo:nil repeats:NO];
//            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
//            self.minShowTimer = timer;
//            return;
//        }
//    }
//    // ... otherwise hide the HUD immediately
//    [self hideUsingAnimation:self.useAnimation];
//}
//
//- (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)delay {
//    NSTimer *timer = [NSTimer timerWithTimeInterval:delay target:self selector:@selector(handleHideTimer:) userInfo:@(animated) repeats:NO];
//    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
//    self.hideDelayTimer = timer;
//}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
